import json
from myApp.models import *

def getAllData():
    def map_fn(item):
        # 类型
        item.type=item.type.split(',')

        if item.country==None:
            item.country='中国大陆'
        else:
            item.country=item.country.split(',')

        if item.language==None:
            item.language='汉语普通话'
        else:
            item.language=item.language.split(',')
        #星级
        item.starts = item.starts.split('%.')
        def add_a(list):
            return [x + '%' for x in list]
        item.starts = add_a(item.starts)

        #图片
        item.imglist = item.imglist.split('.https')
        def fix_urls(url_list):
            fixed = []
            for i, url in enumerate(url_list):
                if i == 0:
                    fixed.append(url)
                else:
                    fixed.append(f"https{url}")
            return fixed
        item.imglist = fix_urls(item.imglist)

        item.comments = json.loads(item.comments)
        return item
    allData = Movies.objects.all()
    allData=list(map(map_fn,list(allData)))
    print(allData[0].comments)
    return allData


def getAllCommentsData():
    allData = Movies.objects.all()
    commentList = []
    for movie in allData:
        try:
            comments = json.loads(movie.comments)
            if isinstance(comments, list) and comments:
                for comment_dict in comments:
                    comment_content = comment_dict.get('comments')
                    if comment_content:
                        commentList.append(comment_content)
            else:
                print("评论为空或格式错误")
        except json.JSONDecodeError as e:
            print(f"JSON解析失败: {e}")
        except Exception as e:
            print(f"错误: {e}")
    return commentList

# def getAllCommentsDataTwo():
#     commentsData = Comments.objects.all()
#     commentList = []
#     for i in commentsData:
#         commentList.append(i.commentContent)
#     print(commentList[0])

